package com.TIEmulator;

import android.util.Log;
import java.nio.ByteBuffer;

public final class TIEmulatorInterface
{
  private static int EMU_HEIGHT = 256;
  private static int EMU_WIDTH = 480;
  private static ByteBuffer byteBuffer = ByteBuffer.allocateDirect(2 * EMU_HEIGHT * EMU_WIDTH);
  private static boolean is_shutdown;
  private static Object listener = null;
  private static final String logID = TIEmulatorInterface.class.toString();

  static
  {
    is_shutdown = false;
  }

  public static boolean LoadLib()
  {
    int i;
    try
    {
      System.loadLibrary("TIEmulator");
      i = 1;
      return i;
    }
    catch (UnsatisfiedLinkError localUnsatisfiedLinkError)
    {
      Log.e(logID, localUnsatisfiedLinkError.toString());
      i = 0;
    }
  }

  private static void OnFatalError(String paramString)
  {
    if (listener instanceof BaseInterface)
      ((BaseInterface)listener).OnFatalError(paramString);
    while (true)
    {
      return;
      ThrowInterfaceNotBoundException("NativeEvents");
    }
  }

  private static void OnImageUpdate()
  {
    if (listener instanceof NativeEvents)
      ((NativeEvents)listener).OnImageUpdate(byteBuffer);
    while (true)
    {
      return;
      ThrowInterfaceNotBoundException("NativeEvents");
    }
  }

  private static void OnTIEmuStopped()
  {
    if (listener instanceof NativeEvents)
      ((NativeEvents)listener).OnTIEmuStopped();
    while (true)
    {
      return;
      ThrowInterfaceNotBoundException("NativeEvents");
    }
  }

  private static void ThrowInterfaceNotBoundException(String paramString)
  {
    if (is_shutdown)
      return;
    throw new InterfaceNotBoundException(paramString);
  }

  public static native boolean postKeyCode(int paramInt1, int paramInt2);

  public static void setListener(Object paramObject)
  {
    if ((paramObject instanceof BaseInterface) || (paramObject instanceof NativeTestFilesEvents) || (paramObject instanceof NativeEvents))
    {
      listener = paramObject;
      return;
    }
    listener = null;
    throw new CannotBindInterfaceException(paramObject);
  }

  public static boolean startEmulate()
  {
    return startEmulation(byteBuffer);
  }

  private static native boolean startEmulation(ByteBuffer paramByteBuffer);

  public static void stopEmulate()
  {
    is_shutdown = true;
    listener = null;
    stopEmulation();
  }

  private static native void stopEmulation();

  public static abstract interface BaseInterface
  {
    public abstract void OnFatalError(String paramString);
  }

  public static class CannotBindInterfaceException extends RuntimeException
  {
    private static final long serialVersionUID = 1L;

    public CannotBindInterfaceException(Object paramObject)
    {
      super("Cannot bind Object: " + paramObject.toString() + "to any internal Interface!");
    }
  }

  public static class InterfaceNotBoundException extends RuntimeException
  {
    private static final long serialVersionUID = 1L;

    public InterfaceNotBoundException(String paramString)
    {
      super("TIEmulatorInterface <" + paramString + "> not bound internaly!");
    }
  }

  public static abstract interface NativeEvents extends TIEmulatorInterface.BaseInterface
  {
    public abstract void OnImageUpdate(ByteBuffer paramByteBuffer);

    public abstract void OnTIEmuStopped();
  }

  public static abstract interface NativeTestFilesEvents extends TIEmulatorInterface.BaseInterface
  {
    public abstract void OnTestResult(String paramString);
  }
}
